package com.cnzha.mr.order;

import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.util.Objects;

/**
 * @Author: zhaoxin
 * @email: zdkk@foxmail.com
 * @Date: 2020/4/4 18:41
 */
public class OrderBean implements WritableComparable<OrderBean> {

    private Long orderId;
    private Double price;

    public Long getOrderId() {
        return orderId;
    }

    public void setOrderId(Long orderId) {
        this.orderId = orderId;
    }

    public Double getPrice() {
        return price;
    }

    public void setPrice(Double price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return orderId.toString() + '\t' + price;
    }

    @Override
    public int compareTo(OrderBean bean) {
        Long orderId = bean.getOrderId();
        Double price = bean.getPrice();
        if (Objects.equals(orderId, this.orderId)) {
            //单号相同价格排序
            if (price > this.price) {
                return 1;
            } else if (price < this.price) {
                return -1;
            } else {
                return 0;
            }
        } else {
            if (this.orderId > orderId) {
                return 1;
            } else {
                return -1;
            }
        }
    }

    @Override
    public void write(DataOutput out) throws IOException {
        out.writeLong(orderId);
        out.writeDouble(price);
    }

    @Override
    public void readFields(DataInput in) throws IOException {
        orderId = in.readLong();
        price = in.readDouble();

    }
}
